package com.anhry.www.safety.manage.enterprise.struts.action;

import java.io.File;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.www.safety.manage.enterprise.bean.TbCorpInfo;
import com.anhry.www.safety.manage.enterprise.bean.TbCorpPic;
import com.anhry.www.safety.manage.enterprise.struts.base.EnterpriseBaseAction;
import com.anhry.www.safety.util.CompressPic;
import com.anhry.www.safety.util.Environment;
import com.anhry.www.safety.util.Loggers;
import com.anhry.www.safety.util.UploadFile;
import com.anhry.www.safety.util.Util;
import com.anhry.www.safety.util.UtilMethod;

/**
 * 
 * @author chen
 * @date 2012-9-10
 */
@Controller
@Scope("prototype")
@SuppressWarnings("unchecked")
public class CorpPicAction extends EnterpriseBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String corpId;

	private String picType;// 图片类型

	private String picTitle;// 标题

	private String picRemark;// 图片说明

	private File pic;

	private String picContentType;

	private String picFileName;
	
	private String picId;

	private TbCorpPic corpPic;
	
	private String customType; 
	
	private static final Environment env = Environment.getInstance();
	
	/**
	 * 企业图片
	 * 
	 * @return
	 */
	public String list() {
		try {
			TbCorpInfo sessioncorp = (TbCorpInfo) contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
			if (sessioncorp != null) {
				this.setObjList((List) this.utilService.getHqlQuery("from TbCorpPic t where 1=1 and t.corpId="+ sessioncorp.getId() + " order by t.id"));
				this.setResult("corp_pic_list");
				return SUCCESS;
			}

		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpPicAction:lookPicList() 异常："
					+ e.getMessage());
		}
		return ERROR;
	}

	/**
	 * 图片录入页面
	 * 
	 * @return
	 */
	public String add() {
		this.setResult("corp_pic_add");
		return SUCCESS;
	}
	
	/**
	 * 图片修改页面
	 * 
	 * @return
	 */
	public String edit() {
		try {
			if(picId!=null&&!"".equals(picId)){
				this.corpPic=(TbCorpPic)this.getUtilService().getObjectById(TbCorpPic.class, Long.parseLong(picId) );
			}
			this.setResult("corp_pic_edit");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpPicAction:edit() 异常："+ e.getMessage());
		}
		
		return ERROR;
	}

	/**
	 * 添加图片
	 * 
	 * @return
	 */
	public String save() {
		try {
			TbCorpInfo sessioncorp = (TbCorpInfo) contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
			
			if (null != sessioncorp && sessioncorp.getId()!=null) {
				this.corpId=sessioncorp.getId().toString();
				if (null != this.pic) {
					if (!UtilMethod.filterType(this.picContentType,Util.ALLOWTYPES.split(","))) {
						this.setTip("图片格式不正确,请选择正确的图片格式!");
						return this.add();
					} else {
						String path = Util.SAFETY_FILE + "/"+ Util.FILE_FOLDER + "/" + Util.CORP;
						String pathDest = Util.SAFETY_FILE + "/"+ Util.FILE_FOLDER + "/" + Util.CORP_RESIZE;
						//System.out.println(path);
						String fileName = UploadFile.copy(this.pic, path,this.picFileName, this.corpId);
						if(pic.length() > env.getIntProperty("upload.corp.pics.resize.limit")) {
							CompressPic comp = new CompressPic();
							comp.compressPic(path +"/" + corpId +"/", pathDest+"/" + corpId +"/",
									fileName, "R_" + fileName, env.getIntProperty("upload.corp.pics.limit.width"),
									env.getIntProperty("upload.corp.pics.limit.height"), true	);
						}
						String path1 = Util.FILE_FOLDER + "/"+ Util.CORP_RESIZE + "/"+ this.corpId + "/"+ "R_" + fileName;
					
						TbCorpPic pic = new TbCorpPic();
						pic.setIsDele("0");
						pic.setPicPath(path1);
						pic.setCustomType(customType);
						pic.setPicRemark(this.picRemark);
						pic.setPicTitle(this.picTitle);
						pic.setPicType(this.picType);
						pic.setCorpId(Long.parseLong(this.corpId));
						this.utilService.save(pic);
						this.setTip("添加成功！");
					}
				}
				return this.list();
			}
		} catch (Exception e) {
			Loggers.error("manager:CorpPicAction:addPic()方法出错：" + e.getMessage());
			e.printStackTrace();
		}
		this.setTip(Util.NOTIP);
		return ERROR;
	}
	/**
	 * 修改图片信息
	 * 
	 * @return
	 */
	public String editPic() {
		try {
			
			TbCorpInfo sessioncorp = (TbCorpInfo) contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
			if (null != sessioncorp && sessioncorp.getId()!=null) {
				this.corpId=sessioncorp.getId().toString();
			}
			if (null != picId && !"".equals(picId)) {
				
				TbCorpPic oldCorpPic=(TbCorpPic)this.getUtilService().getObjectById(TbCorpPic.class, Long.parseLong(picId)); 
				
				if (null != this.pic) {
					if (!UtilMethod.filterType(this.picContentType,
							Util.ALLOWTYPES.split(","))) {
						this.setTip("图片格式不正确,请选择正确的图片格式!");
						return this.edit();
					} else {
						/*String path = Util.SAFETY_FILE + "/"
								+ Util.FILE_FOLDER + "/"+ Util.CORP;
						String fileName = UploadFile.copy(this.pic, path,
								this.picFileName, this.corpId);
						String path1 = Util.FILE_FOLDER + "/"
								+ Util.CORP + "/"+ this.corpId
								+ "/"+ fileName;*/
						String path = Util.SAFETY_FILE + "/"+ Util.FILE_FOLDER + "/" + Util.CORP;
						String pathDest = Util.SAFETY_FILE + "/"+ Util.FILE_FOLDER + "/" + Util.CORP_RESIZE;
						//System.out.println(path);
						String fileName = UploadFile.copy(this.pic, path,this.picFileName, this.corpId);
						if(pic.length() > env.getIntProperty("upload.corp.pics.resize.limit")) {
							CompressPic comp = new CompressPic();
							comp.compressPic(path +"/" + corpId +"/", pathDest+"/" + corpId +"/",
									fileName, "R_" + fileName, env.getIntProperty("upload.corp.pics.limit.width"),
									env.getIntProperty("upload.corp.pics.limit.height"), true	);
						}
						String path1 = Util.FILE_FOLDER + "/"+ Util.CORP_RESIZE + "/"+ this.corpId + "/"+ "R_" + fileName;
						oldCorpPic.setPicPath(path1);
						
					}
				}
				oldCorpPic.setPicRemark(this.picRemark);
				oldCorpPic.setCustomType(customType);
				oldCorpPic.setPicTitle(this.picTitle);
				oldCorpPic.setPicType(this.picType);
				this.utilService.update(oldCorpPic);
				this.setTip("修改成功！");
				return this.list();
			}
		} catch (Exception e) {
			Loggers.error("manager:CorpPicAction:editPic()方法出错：" + e.getMessage());
			e.printStackTrace();
		}
		this.setTip(Util.NOTIP);
		return ERROR;
	}
	/**
	 * 删除
	 * 
	 */
	public String delPic() {
		this.contextPvd.getResponse().setContentType("charset=UTF-8");
		try {
			if (null != this.picId && this.picId.length() > 0) {
				TbCorpPic delpic = (TbCorpPic) this.utilService.getObjectById(TbCorpPic.class, Long.parseLong(this.picId));
				if (null != delpic) {
					UploadFile.removeFile(Util.SAFETY_FILE + "/"+ delpic.getPicPath());
					this.utilService.delete(delpic);
					this.setTip("删除成功！");
					return this.list();
				}
			}  
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("manager:CorpPicAction:delPic()方法出错："+ e.getMessage());
		}
		return ERROR;
	}

	public String getCorpId() {
		return corpId;
	}

	public void setCorpId(String corpId) {
		this.corpId = corpId;
	}

	public String getPicType() {
		return picType;
	}

	public void setPicType(String picType) {
		this.picType = picType;
	}

	public String getPicTitle() {
		return picTitle;
	}

	public void setPicTitle(String picTitle) {
		this.picTitle = picTitle;
	}

	public String getPicRemark() {
		return picRemark;
	}

	public void setPicRemark(String picRemark) {
		this.picRemark = picRemark;
	}

	public File getPic() {
		return pic;
	}

	public void setPic(File pic) {
		this.pic = pic;
	}

	public String getPicContentType() {
		return picContentType;
	}

	public void setPicContentType(String picContentType) {
		this.picContentType = picContentType;
	}

	public String getPicFileName() {
		return picFileName;
	}

	public void setPicFileName(String picFileName) {
		this.picFileName = picFileName;
	}

	public String getPicId() {
		return picId;
	}

	public void setPicId(String picId) {
		this.picId = picId;
	}

	public TbCorpPic getCorpPic() {
		return corpPic;
	}

	public void setCorpPic(TbCorpPic corpPic) {
		this.corpPic = corpPic;
	}

	public String getCustomType() {
		return customType;
	}

	public void setCustomType(String customType) {
		this.customType = customType;
	}

}
